package com.zb.sinocare.detection.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zb.sinocare.common.domain.vo.PatientVo;
import com.zb.sinocare.common.result.Result;
import com.zb.sinocare.common.result.ResultCodeEnum;
import com.zb.sinocare.detection.service.PatientService;
import com.zb.sinocare.common.domain.Patient;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin     //解决跨域
@RequestMapping("/patient")
public class PatientController {
    //引入service
    @Autowired
    private PatientService patientService;

    @RequestMapping(value = "find",method = RequestMethod.GET)
    @ApiOperation(value = "查询检测者",notes = "根据条件分页查询",httpMethod = "GET")
    public Result findPatients(Integer page,Integer limit,String searchParams) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        PatientVo search = null;
        if (searchParams!=null &&"".equals(searchParams)){
            search = objectMapper.readValue(searchParams,PatientVo.class);
            System.out.println(search);
        }
        Page page1 = patientService.selectListPage(page, limit, search);
        //List<Patient> patients = patientService.list();
        return Result.ok(ResultCodeEnum.SUCCESS,page1.getTotal(),page1.getRecords());
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ApiOperation(value = "添加检测者",notes = "添加检测者",httpMethod = "POST")
    public Result addPatients(Patient patient) throws Exception{
        System.out.println(patient);
        int i = patientService.addPatient(patient);
        if (i>0){
            return Result.ok(ResultCodeEnum.SUCCESS,i);
        }else{
            return Result.ok(ResultCodeEnum.FAIL,null);
        }

    }

    @RequestMapping(value = "{patientId}", method = RequestMethod.DELETE)
    @ApiOperation(value = "根据ID删除检测者", notes = "根据ID删除检测者", httpMethod = "DELETE")
    public Result deleteUserById(@PathVariable("patientId") Integer patientId) throws Exception{
        //TODO:service方法
        int i = patientService.deletePatientById(patientId);
        if(i>0){
            return Result.ok(ResultCodeEnum.SUCCESS, i);
        }else{
            return Result.ok(ResultCodeEnum.FAIL, null);
        }

    }

}
